package com.llvision.ndkdemoprocessdemo.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.Process;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;

import com.llvision.ndkdemoprocessdemo.MainActivity;
import com.llvision.ndkdemoprocessdemo.R;
import com.llvision.ndkdemoprocessdemo.utils.NdkUtil;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MyLogService extends Service {

    private static final String TAG = MyLogService.class.getSimpleName();
    private final int id = 1;
    private Notification notification;
    private final String channelId = "com.llvision.ndkdemoprocessdemo";

    private ScheduledExecutorService mScheduledService;

    public MyLogService() {
        Log.w(TAG, "MyLogService constructor");

        this.mScheduledService = Executors.newScheduledThreadPool(1);
        mScheduledService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                Log.w(TAG, "MyLogService -> " + (new Date()));
            }
        }, 5000, 2000, TimeUnit.MILLISECONDS);
        NdkUtil.nativeProtect(String.valueOf(Process.myPid()));

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder;
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final NotificationChannel notificationChannel = new NotificationChannel(channelId,"logger_channel_name",NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            builder = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("NDK")
                    .setContentText("running")
                    .setContentIntent(pendingIntent)
                    .setCategory(NotificationCompat.CATEGORY_SERVICE)
                    .setOngoing(true);
            notification = builder.build();
            startForeground(id, notification);
            notificationManager.notify(id, notification);

        }
        return super.onStartCommand(intent, flags, startId);
    }
}
